package com.tianbing.adlottery.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.ICategoryBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.ISystemDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Category2Category;
import com.fengniu.ejb.entity.Content2Category;
import com.fengniu.ejb.entity.Site2Category;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class CategoryController extends MultiActionController {
	Logger logger = Logger.getLogger(CategoryController.class.getName());

	private String category2ProductListView;

	private String ajaxCategory2ProductListView;

	private String category2StoreListView;

	private String ajaxCategory2StoreListView;

	private String category2HordeListView;

	private String ajaxCategory2HordeListView;

	private String category2WebSiteListView;

	private String ajaxCategory2WebSiteListView;

	private String ajaxHotCategoryListView;

	private ISiteDao siteDao;

	private IUserDao userDao;

	private IContentDao contentDao;

	private ICategoryDao categoryDao;

	private ICategoryBusiness categoryBusiness;

	private SkinUtils skinUtils;

	private ISystemDao systemDao;

	private int productPageSize = 16;

	private int storePageSize = 20;

	/**
	 * list products
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ModelAndView listCategory2Product(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String method = ServletRequestUtils.getStringParameter(request, "method", null);
		if (method != null && method.equals("ajax")) {
			return ajaxListCategory2Product(request, response);
		} else {
			ModelAndView mv = new ModelAndView(category2ProductListView);
			Map model = mv.getModel();
			// --get request parameter
			String cgid = ServletRequestUtils.getStringParameter(request, "cgid", IConstants.CategoryID.CONTENT_TOP
					.getId());
			String brdgid = ServletRequestUtils.getStringParameter(request, "brdgid", null); // brand category Id
			if (brdgid == null || brdgid.trim().equals(""))
				brdgid = null;

			Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
			Integer pgsize = productPageSize;// ServletRequestUtils.getIntParameter(request,
			// "pgsize", 30);

			// ----------------------get data----------------------
			Map c2gMap = categoryBusiness.getCategoryProducts(cgid, brdgid, pgno, pgsize);
			List<Content2Category> content2CategoryList = (List<Content2Category>) c2gMap.get("content2CategoryList");
			Long totalCount = (Long) c2gMap.get("totalCount");
			Category contentCategory = (Category) c2gMap.get("contentCategory");
			Category contentBrandCategory = (Category) c2gMap.get("brandCategory");

			List<Category> primaryContentCategoryTrail = (List<Category>) c2gMap.get("primaryContentCategoryTrail");
			List<Category> childContentCategoryList = (List<Category>) c2gMap.get("childContentCategoryList");

			// get related brand categories
			List<Category2Category> contentBrandCategoryList = categoryBusiness.getListCategoryRelatedBrand(cgid);
			// -------/get data--------------------------------------------

			// ------------------------format data---------------------
			List content2CategoryGroupList = UtilDataGroup.makeGroups(content2CategoryList, 0, 4);
			List contentChildCategoryListGroups = UtilDataGroup.makeGroups(childContentCategoryList, 4, 5);
			List contentBrandCategoryListGroups = UtilDataGroup.makeGroups(contentBrandCategoryList, 0, 2);

			// prepare output data map for view-----------------------------
			model.put("contentCategory", contentCategory);
			model.put("contentBrandCategory", contentBrandCategory);

			model.put("primaryContentCategoryTrail", primaryContentCategoryTrail);
			model.put("contentChildCategoryListGroups", contentChildCategoryListGroups);
			model.put("content2CategoryGroupList", content2CategoryGroupList);
			model.put("contentBrandCategoryListGroups", contentBrandCategoryListGroups);
			model.put("pagefooterparas", "cgid=" + cgid);
			model = skinUtils.preparePageFooter(request, content2CategoryList, totalCount, pgno, pgsize, model);
			return mv;
		}
	}

	/**
	 * list page of Category2Product by ajax
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ModelAndView ajaxListCategory2Product(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(ajaxCategory2ProductListView);
		Map<String, Object> model = mv.getModel();
		// --get request parameter
		String cgid = ServletRequestUtils
				.getStringParameter(request, "cgid", IConstants.CategoryID.CONTENT_TOP.getId());
		String brdgid = ServletRequestUtils.getStringParameter(request, "brdgid", null); // brand category Id
		if (brdgid == null || brdgid.trim().equals(""))
			brdgid = null;

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = productPageSize;

		// ------------------------get data-----------------------------------
		Map<String, Object> c2gMap = categoryBusiness.getCategoryProducts(cgid, brdgid, pgno, pgsize);
		List<Content2Category> content2CategoryList = (List<Content2Category>) c2gMap.get("content2CategoryList");

		List content2CategoryGroupList = UtilDataGroup.makeGroups(content2CategoryList, 0, 4);
		// prepare output data map for view
		model.put("pgno", pgno);
		model.put("content2CategoryGroupList", content2CategoryGroupList);
		return mv;
	}

	/**
	 * list retail stores
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView listCategory2Store(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(category2StoreListView);
		Map model = mv.getModel();
		// --get request parameter
		// get gid from parameter
		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", IConstants.CategoryID.AREA_TOP.getId());
		String bizgid = ServletRequestUtils.getStringParameter(request, "bizgid", null);
		if (bizgid == null || bizgid.trim().equals(""))
			bizgid = null;
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = storePageSize;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		Map<String, Object> s2gMap = categoryBusiness.getCategoryStores(sgid, bizgid, pgno, pgsize);
		Category siteCategory = (Category) s2gMap.get("siteCategory");
		Category businessCategory = (Category) s2gMap.get("businessCategory");
		List<Site2Category> site2CategoryList = (List<Site2Category>) s2gMap.get("site2CategoryList");
		Long totalCount = (Long) s2gMap.get("totalCount");
		List<Category> childSiteCategoryList = (List<Category>) s2gMap.get("childSiteCategoryList");
		List<Category> primarySiteCategoryTrail = (List<Category>) s2gMap.get("primarySiteCategoryTrail");

		List site2CategoryGroupList = UtilDataGroup.makeGroups(site2CategoryList, 0, 4);
		List siteChildCategoryListGroups = UtilDataGroup.makeGroups(childSiteCategoryList, 0, 8);
		// prepare output data map for view
		model.put("siteCategory", siteCategory);
		model.put("businessCategory", businessCategory);
		model.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
		model.put("siteChildCategoryListGroups", siteChildCategoryListGroups);
		model.put("site2CategoryGroupList", site2CategoryGroupList);
		model.put("pagefooterparas", "sgid=" + sgid);
		model = skinUtils.preparePageFooter(request, site2CategoryList, totalCount, pgno, pgsize, model);
		return mv;
	}

	/**
	 * list page of Category2stores by ajax
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView ajaxListCategory2Store(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(ajaxCategory2StoreListView);
		Map model = mv.getModel();
		// --get request parameter
		// get gid from parameter
		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", IConstants.CategoryID.AREA_TOP.getId());
		String bizgid = ServletRequestUtils.getStringParameter(request, "bizgid", IConstants.CategoryID.BUSINESS_TOP
				.getId());
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = storePageSize;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		Map<String, Object> s2gMap = categoryBusiness.getCategoryStores(sgid, bizgid, pgno, pgsize);
		List<Site2Category> site2CategoryList = (List<Site2Category>) s2gMap.get("site2CategoryList");

		List site2CategoryGroupList = UtilDataGroup.makeGroups(site2CategoryList, 0, 4);

		// prepare output data map for view
		model.put("pgno", pgno);
		model.put("site2CategoryGroupList", site2CategoryGroupList);
		return mv;
	}

	/**
	 * list retail stores
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ModelAndView listCategory2Horde(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(category2HordeListView);
		Map model = mv.getModel();
		// --get request parameter
		// get gid from parameter
		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", IConstants.CategoryID.AREA_TOP.getId());
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = storePageSize;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		Map<String, Object> s2gMap = categoryBusiness.getCategoryHordes(sgid, null, pgno, pgsize);
		Category siteCategory = (Category) s2gMap.get("siteCategory");
		List<Site2Category> site2CategoryList = (List<Site2Category>) s2gMap.get("site2CategoryList");
		Long totalCount = (Long) s2gMap.get("totalCount");
		List<Category> childSiteCategoryList = (List<Category>) s2gMap.get("childSiteCategoryList");
		List<Category> primarySiteCategoryTrail = (List<Category>) s2gMap.get("primarySiteCategoryTrail");

		List site2CategoryGroupList = UtilDataGroup.makeGroups(site2CategoryList, 0, 4);
		List siteChildCategoryListGroups = UtilDataGroup.makeGroups(childSiteCategoryList, 0, 8);

		// prepare output data map for view
		model.put("siteCategory", siteCategory);
		model.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
		model.put("siteChildCategoryListGroups", siteChildCategoryListGroups);
		model.put("site2CategoryGroupList", site2CategoryGroupList);
		model.put("pagefooterparas", "sgid=" + sgid);
		model = skinUtils.preparePageFooter(request, site2CategoryList, totalCount, pgno, pgsize, model);
		return mv;
	}

	/**
	 * list page of Category2stores by ajax
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView ajaxListCategory2Horde(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(ajaxCategory2HordeListView);
		Map model = mv.getModel();
		// --get request parameter
		// get gid from parameter
		String sgid = ServletRequestUtils.getStringParameter(request, "sgid", IConstants.CategoryID.AREA_TOP.getId());
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = storePageSize;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		Map<String, Object> s2gMap = categoryBusiness.getCategoryHordes(sgid, null, pgno, pgsize);

		List<Site2Category> site2CategoryList = (List<Site2Category>) s2gMap.get("site2CategoryList");
		List site2CategoryGroupList = UtilDataGroup.makeGroups(site2CategoryList, 0, 4);

		// prepare output data map for view
		model.put("pgno", pgno);
		model.put("site2CategoryGroupList", site2CategoryGroupList);
		return mv;
	}

	public ModelAndView ajaxListHotCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(ajaxHotCategoryListView);
		Map model = mv.getModel();
		return mv;
	}

	/**
	 * list retail stores
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView listCategory2WebSite(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(category2WebSiteListView);
		Map model = mv.getModel();
		// --get request parameter
		// get gid from parameter
		String sgid = ServletRequestUtils
				.getStringParameter(request, "sgid", IConstants.CategoryID.WEBSITE_TOP.getId());
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = storePageSize;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		Map<String, Object> s2gMap = categoryBusiness.getCategoryWebSites(sgid, null, pgno, pgsize);
		Category siteCategory = (Category) s2gMap.get("siteCategory");
		List<Site2Category> site2CategoryList = (List<Site2Category>) s2gMap.get("site2CategoryList");
		Long totalCount = (Long) s2gMap.get("totalCount");
		List<Category> childSiteCategoryList = (List<Category>) s2gMap.get("childSiteCategoryList");
		List<Category> primarySiteCategoryTrail = (List<Category>) s2gMap.get("primarySiteCategoryTrail");

		List site2CategoryGroupList = UtilDataGroup.makeGroups(site2CategoryList, 0, 4);
		List siteChildCategoryListGroups = UtilDataGroup.makeGroups(childSiteCategoryList, 0, 9);

		// prepare output data map for view
		model.put("siteCategory", siteCategory);
		model.put("primarySiteCategoryTrail", primarySiteCategoryTrail);
		model.put("siteChildCategoryListGroups", siteChildCategoryListGroups);
		model.put("site2CategoryGroupList", site2CategoryGroupList);
		model.put("pagefooterparas", "sgid=" + sgid);
		model = skinUtils.preparePageFooter(request, site2CategoryList, totalCount, pgno, pgsize, model);
		return mv;
	}

	/**
	 * list page of Category2stores by ajax
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView ajaxListCategory2WebSite(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(ajaxCategory2WebSiteListView);
		Map model = mv.getModel();
		// --get request parameter
		// get gid from parameter
		String sgid = ServletRequestUtils
				.getStringParameter(request, "sgid", IConstants.CategoryID.WEBSITE_TOP.getId());
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = storePageSize;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		Map<String, Object> s2gMap = categoryBusiness.getCategoryWebSites(sgid, null, pgno, pgsize);

		List<Site2Category> site2CategoryList = (List<Site2Category>) s2gMap.get("site2CategoryList");
		List site2CategoryGroupList = UtilDataGroup.makeGroups(site2CategoryList, 0, 4);

		// prepare output data map for view
		model.put("pgno", pgno);
		model.put("site2CategoryGroupList", site2CategoryGroupList);
		return mv;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setCategory2ProductListView(String homeView) {
		this.category2ProductListView = homeView;
	}

	public void setCategory2StoreListView(String category2WordListView) {
		this.category2StoreListView = category2WordListView;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setSystemDao(ISystemDao systemDao) {
		this.systemDao = systemDao;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setAjaxCategory2ProductListView(String ajaxCategory2ProductListView) {
		this.ajaxCategory2ProductListView = ajaxCategory2ProductListView;
	}

	public void setAjaxCategory2StoreListView(String ajaxCategory2StoreListView) {
		this.ajaxCategory2StoreListView = ajaxCategory2StoreListView;
	}

	public void setCategory2HordeListView(String category2HordeListView) {
		this.category2HordeListView = category2HordeListView;
	}

	public void setAjaxCategory2HordeListView(String ajaxCategory2HordeListView) {
		this.ajaxCategory2HordeListView = ajaxCategory2HordeListView;
	}

	public void setProductPageSize(int productPageSize) {
		this.productPageSize = productPageSize;
	}

	public void setStorePageSize(int storePageSize) {
		this.storePageSize = storePageSize;
	}

	public void setCategory2WebSiteListView(String category2WebSiteListView) {
		this.category2WebSiteListView = category2WebSiteListView;
	}

	public void setAjaxCategory2WebSiteListView(String ajaxCategory2WebSiteListView) {
		this.ajaxCategory2WebSiteListView = ajaxCategory2WebSiteListView;
	}

	public void setCategoryBusiness(ICategoryBusiness categoryBusiness) {
		this.categoryBusiness = categoryBusiness;
	}

	public void setAjaxHotCategoryListView(String ajaxHotCategoryListView) {
		this.ajaxHotCategoryListView = ajaxHotCategoryListView;
	}

}
